//BASE_URL = "https://winsglobal.com/mobileapp/mobile_api/";
BASE_URL = "http://192.168.10.16/wnsgbl-repo/mobileapp/mobile_api/";
BASE_URL2 = "http://192.168.10.16/wnsgbl-repo/mobileapp/"; 
BASE_URL3 = "http://192.168.10.16/wnsgbl-repo/static/uploads/";
//BASE_URL3 = "https://winsglobal.s3.amazonaws.com/uploads/";
//BASE_URL4 = "https://www.winsglobal.com/";
BASE_URL4 = "http://192.168.10.16/";
var checkout_win = null;

var app = new $.mvc.app();
 
 
app.loadControllers("event");
app.loadModels("event");
app.loadControllers("payment");  
app.loadModels("payment"); 
app.loadControllers("myticket"); 
app.loadModels("myticket");
app.loadControllers("myshop");
app.loadModels('myshop'); 
app.loadControllers("categories");
app.loadModels('categories'); 


var token = "";
var error = ""; 
var user = ""; 

$.fn.clearForm = function() {
  return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
      return $(':input',this).clearForm();
    if (type == 'text' || type == 'password' || tag == 'textarea' || type == 'hidden')
      this.value = '';
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    else if (tag == 'select')
      this.value = '-1';
  });
};
 
function login() {
    var username = $('#email').val();
    var password = $('#password').val();
    if (!username || !password) {
        $('#errorMessage').html = "Please fill Email and Password";
    } else {
        $.ajax({
            type:"POST",
            url:BASE_URL+"login",
            data:{username:username, password: password},
            success:function(jsonData){
                jsonData = JSON.parse(jsonData);
                if (jsonData.status) {
                    token = jsonData.token;
                    user = jsonData.user;
                    localStorage.setItem('access_token_event', token); 
                    localStorage.setItem('userID', JSON.stringify(user.userID));
                    localStorage.setItem('user_event', JSON.stringify(user));
                    $.mvc.route("/event");
                    $.ui.loadContent("#main",false,false,"slide");
                    $('#email').val('');
                    $('#password').val('');
                } else {
                    error = jsonData.error;
                    $('#errorMessage').html = error;
                    alert('wrong username password ');
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
            }
        });
    }
}

function logout() {
    clearAllData();
    localStorage.clear();
    $.ui.loadContent("#select",false,false,"up");
}

function myticket_action() { 
    if(localStorage.getItem("userID")==null){
            
            $.ui.loadContent("#select",false,false,"up");
            
        }else{
    $.mvc.route("/myticket");
    $.ui.loadContent("#myticket",false,false,"up");
    
        }
}

function myshop_action() { 
    if(localStorage.getItem("userID")==null){
            
            $.ui.loadContent("#select",false,false,"up");
            
        }else{
    $.mvc.route("/myshop");
    $.ui.loadContent("#myticket",false,false,"up");
    
        }
}
 
function clearAllData() {
    localStorage.removeItem('access_token_event');
    localStorage.removeItem('user_event');
    user = "";
    token = "";
}

//We wait until app.ready is available to fetch the data, then we wire up the existing data in the templates
 
app.ready(function(){
   // if(localStorage.getItem('access_token_event')) {
        
        //$.mvc.route("/event");
        
        $.mvc.route("/categories");

        $.ui.loadContent("#main",false,false,"slide");
   // } else {
    //    alert('there is no token');
    //    setTimeout(function () {
   //         $.ui.loadContent("#select",false,false,"up");
   //     }, 500);
   // }
});

/*function buy(id) {
    //var uri = encodeURIComponent(email);
    //window.open('http://192.168.10.21/wnsgbl-repo/mobileapp/auto_login/'+uri);
    $.get(BASE_URL+"event?id="+encodeURIComponent(id),
        function(obj) {
            obj = JSON.parse(obj);
            addToCart(obj);
        }
    );
}*/
 

function addToCart(item) {
     
    //alert(item[0].eventpackageID);
      
    if (localStorage.getItem('shopping_cart')) {
        
        var carts = JSON.parse(localStorage.getItem('shopping_cart')); 
         
        var status = false;
        if (carts.length > 0) {
            for(var i=0; i< carts.length; i++){
                
                //alert(carts[i][0].eventpackageID);
                
                //alert(item[0].eventpackageID);
                
                if (carts[i][0].eventpackageID === item[0].eventpackageID) {
                     
                    carts[i].cart_qty++;
                    status = true;
                    break;
                    
                }
            }
        }        
       
        if (!status) { 
            item[0].cart_qty = 1;
            carts.push(item);
        }
         
        // alert(JSON.stringify(carts));
         
        localStorage.setItem('shopping_cart', JSON.stringify(carts));
        
        
    } else {
        
        var carts = [];
        item[0].cart_qty = 1;
        carts.push(item);
        // alert('baru');
        //alert(JSON.stringify(item));
        localStorage.setItem('shopping_cart', JSON.stringify(carts));
        
    }
    
     //var allcookies = localStorage.getItem('shopping_cart');
     //alert(allcookies);
}
     
function goToCart() {
    $.ui.loadContent("#cart_panel",false,false,"slide");
}

function updateQty(id, qty) {
    $.mvc.route('/event/updateCart/'+id+'/'+qty);
}

function openPaymentWindow() {
    var carts = JSON.parse(localStorage.getItem('shopping_cart'));
    var status = false;
    var queryString = "";
    if (carts.length > 0) {
        for(var i=0; i< carts.length; i++){
            queryString = queryString + '&eventpackageID[]=' + carts[i].eventpackageID;
        }
    } else {
        alert('Shopping cart is empty');
    }
    
    checkout_win = window.open(BASE_URL2+'deal_payment?email=cahyo.leksono%40gmail.com'+queryString,'location=yes');
    
    checkout_win.addEventListener('loadstart', function(event) {
        if (event.url.match("mobile/close")) {
            checkout_win.close();
        }
    });
}

function checkout() {
     
    if(localStorage.getItem("user_event")){
        
        $.ui.loadContent("#payment_panel",false,false,"slide");
        $.mvc.route("/payment");
    
         
    }else{
        
         $.ui.loadContent("#select",false,false,"up");
        
    }
    
}

